package cn.iocoder.yudao.module.pay.controller.admin.order;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.pay.controller.admin.order.vo.HuifuOrderPageReqVO;
import cn.iocoder.yudao.module.pay.controller.admin.order.vo.HuifuOrderRespVO;
import cn.iocoder.yudao.module.pay.controller.admin.order.vo.HuifuOrderSaveReqVO;
import cn.iocoder.yudao.module.pay.dal.dataobject.order.HuifuOrderDO;
import cn.iocoder.yudao.module.pay.service.order.HuifuOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 汇付对帐订单")
@RestController
@RequestMapping("/pay/huiFu/order")
@Validated
public class HuifuOrderController {

    @Resource
    private HuifuOrderService orderService;

    @PostMapping("/create")
    @Operation(summary = "创建汇付对帐订单")
    @PreAuthorize("@ss.hasPermission('pay:order:create')")
    public CommonResult<Integer> createOrder(@Valid @RequestBody HuifuOrderSaveReqVO createReqVO) {
        return success(orderService.createOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新汇付对帐订单")
    @PreAuthorize("@ss.hasPermission('pay:order:update')")
    public CommonResult<Boolean> updateOrder(@Valid @RequestBody HuifuOrderSaveReqVO updateReqVO) {
        orderService.updateOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除汇付对帐订单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pay:order:delete')")
    public CommonResult<Boolean> deleteOrder(@RequestParam("id") Integer id) {
        orderService.deleteOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得汇付对帐订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pay:order:query')")
    public CommonResult<HuifuOrderRespVO> getOrder(@RequestParam("id") Integer id) {
        HuifuOrderDO order = orderService.getOrder(id);
        return success(BeanUtils.toBean(order, HuifuOrderRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得汇付对帐订单分页")
    @PreAuthorize("@ss.hasPermission('pay:order:query')")
    public CommonResult<PageResult<HuifuOrderRespVO>> getOrderPage(@Valid HuifuOrderPageReqVO pageReqVO) {
        PageResult<HuifuOrderDO> pageResult = orderService.getOrderPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, HuifuOrderRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出汇付对帐订单 Excel")
    @PreAuthorize("@ss.hasPermission('pay:order:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOrderExcel(@Valid HuifuOrderPageReqVO pageReqVO,
                                 HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<HuifuOrderDO> list = orderService.getOrderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "汇付对帐订单.xls", "数据", HuifuOrderRespVO.class,
                BeanUtils.toBean(list, HuifuOrderRespVO.class));
    }

}